#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pandas as pd
import codecs
import json
import re
import jieba
import jieba.posseg as pseg


#/Users/wengjunjie/works/人工智能/11data/1632652922606/

fpath = '/Users/wengjunjie/works/人工智能/11data/1632652922606/train.txt'
fout = '/Users/wengjunjie/works/人工智能/11data/1632652922606/test_cleaned.xlsx'


def txt2excel(fp, fout):
    with codecs.open(fp, 'r', encoding='utf8') as f:
        data = []
        i = 0
        for line in f:
            line = line.replace('\\n', '')
            i += 1
            jsonData = {}
            caseCause_pattern = re.compile(r'"caseCause": "(.*?)",')
            caseCause = caseCause_pattern.findall(line)

            justice_pattern = re.compile(r'"justice": "(.*?)",')
            justice = justice_pattern.findall(line)

            opinion_pattern = re.compile(r'"opinion": "(.*?)",')
            opinion = opinion_pattern.findall(line)

            province_pattern = re.compile(r'"province": "(.*?)",')
            province = province_pattern.findall(line)

            judge_pattern = re.compile(r'"judge": (.*?),')
            judge = judge_pattern.findall(line)

            filename_pattern = re.compile(r'"filename": "(.*)"}')
            filename = filename_pattern.findall(line)

            if not max([len(caseCause), len(justice), len(opinion), len(filename)]) == min(
                    [len(caseCause), len(justice), len(opinion), len(filename)]):
                print('{}--------{}'.format(i, line))

            jsonData['caseCause'] = '{}'.format(caseCause[0])
            jsonData['justice'] = '{}'.format(justice[0])
            jsonData['opinion'] = '{}'.format(opinion[0])
            jsonData['province'] = '{}'.format(province[0])
            # jsonData['judge'] = '{}'.format(judge[0])
            jsonData['filename'] = '{}'.format(filename[0])

            data.append(jsonData)
            print('line: {} done'.format(i))

    df = pd.DataFrame(data)
    df.to_excel(fout, encoding='utf8', index=False)


# txt文件转换成excel
txt2excel(fpath, fout)


def distribute_label(fp, fout):
    """
    统计单独属性分布
    """
    dtype = {'judge': str}
    df = pd.read_excel(fp, dtype=dtype)
    s = df['judge'].value_counts()
    s.to_excel(fout, encoding='utf8')


# fp = '/Users/wengjunjie/works/人工智能/11data/1632652922606/train_cleaned.xlsx'
# fout_caseCause = '/Users/wengjunjie/works/人工智能/11data/1632652922606/train_cleaned_judge.xlsx'
# distribute_label(fp, fout_caseCause)
#

def by_group_stat(fp, fout):
    """
    各属性与标签的分布关系
    """
    dtype = {'judge': str}
    df = pd.read_excel(fp, dtype=dtype)
    s = df.groupby(by=['caseCause', 'judge']).size()
    s.to_excel(fout, encoding='utf8')


#
# fp = '/Users/wengjunjie/works/人工智能/11data/1632652922606/train_cleaned.xlsx'
# fout_caseCause_judge = '/Users/wengjunjie/works/人工智能/11data/1632652922606/train_cleaned_caseCause_judge.xlsx'
# by_group_stat(fp, fout_caseCause_judge)

def unique_label(fp, fout):
    """
    统计label类别数
    """
    dtype = {'judge': str}
    df = pd.read_excel(fp, dtype=dtype)
    s = df['judge'].unique()


def txt_length(fp, fout):
    dtype = {'judge': str}
    df = pd.read_excel(fp, dtype=dtype)

    def stat_len(txt):
        txt = '{}'.format(txt)
        if txt == 'nan':
            print(txt)
            return 0
        return len(txt)

    df['caseCause_len'] = df['caseCause'].map(lambda txt: stat_len(txt))
    df['justice_len'] = df['justice'].map(lambda txt: stat_len(txt))
    df['opinion_len'] = df['opinion'].map(lambda txt: stat_len(txt))
    df['province_len'] = df['province'].map(lambda txt: stat_len(txt))
    df['opinion_tiaoli_len'] = df['opinion_tiaoli'].map(lambda txt: stat_len(txt))
    df['txt_len'] = df.apply(
        lambda row: pd.Series(row['caseCause_len'] + row['justice_len'] + row['opinion_tiaoli_len']),
        axis=1)
    df.to_excel(fout, encoding='utf8', index=False)


fp = '/Users/wengjunjie/works/人工智能/11data/1632652922606/train_cleaned_with_label.xlsx'
fout = '/Users/wengjunjie/works/人工智能/11data/1632652922606/train_cleaned_with_label.xlsx'
txt_length(fp, fout)


def gen_label(fp, fout):
    """

    """
    dtype = {'judge': str}
    df = pd.read_excel(fp, dtype=dtype)
    labels = df['judge'].unique().tolist()
    labels = sorted(list(map(int, labels)), reverse=False)
    label2idx = {}
    for idx, label in enumerate(labels):
        label2idx[label] = idx
    df_label = pd.DataFrame.from_dict(label2idx, orient="index")
    df_label.to_excel(fout, encoding='utf8')


# fp = '/Users/wengjunjie/works/人工智能/11data/1632652922606/train_cleaned.xlsx'
# fout = '/Users/wengjunjie/works/人工智能/11data/1632652922606/train_cleaned_label2idx.xlsx'
# gen_label(fp, fout)

def gen_train_label(fp, fout):
    """

    """
    fp_label = '/Users/wengjunjie/works/人工智能/11data/1632652922606/train_cleaned_label2idx.xlsx'
    df_label = pd.read_excel(fp_label)
    names = df_label['name'].values.tolist()
    labels = df_label['label'].values.tolist()
    name2label = {}
    for name, label in zip(names, labels):
        name2label[str(name)] = label

    dtype = {'judge': str}
    df = pd.read_excel(fp, dtype=dtype)
    df['label'] = df['judge'].map(lambda txt: name2label[txt])
    df.to_excel(fout, encoding='utf8', index=False)


#
# fp = '/Users/wengjunjie/works/人工智能/11data/1632652922606/train_cleaned.xlsx'
# fout = '/Users/wengjunjie/works/人工智能/11data/1632652922606/train_cleaned_with_label.xlsx'
# gen_train_label(fp, fout)
#
#

def gen_opinion_tiaoli(fp, fout):
    """
    opinion划分法律条款
    """
    dtype = {'judge': str}
    df = pd.read_excel(fp, dtype=dtype)
    df['opinion_tiaoli'] = df['opinion'].map(lambda txt: '{}'.format(txt).split('。')[-1])
    df.to_excel(fout, encoding='utf8', index=False)


# fp = '/Users/wengjunjie/works/人工智能/11data/1632652922606/train_cleaned_with_label.xlsx'
# fout = '/Users/wengjunjie/works/人工智能/11data/1632652922606/train_cleaned_with_label.xlsx'
# gen_opinion_tiaoli(fp, fout)


def del_txt(fp, fout):
    """
    对人名地名组织名剔除
    """
    dtype = {'judge': str}
    df = pd.read_excel(fp, dtype=dtype)



